<?php
#error_reporting(E_ALL);
# import core libs
include dirname(__FILE__) . '/Core.php';
include dirname(__FILE__) . '/../config/config.conf.php';
sdk_import('@.libraries.SDK_Log');
class ServiceBase
{
    const DEFAULT_CONNECTION_TIME_OUT = 5;
    # Log OBJ
    protected static $logger = NULL;
    # SOAP静态对象
    protected static $soapList = array();
    /**
     * 获取soap对象引用
     * 
     * @param string $wsdl 目标地址
     * @param array $options 参数数组
     * @return mixed
     */
    public static function getSoapInc ($wsdl, $options = array(), &$message)
    {
        $key = md5($wsdl);
        if (! isset(self::$soapList[$key])) {
            if (!self::curlTest(array($wsdl))) {
            	$message = "不能连接到服务 ";
                return false;
            }
            if (! is_array($options)) {
                $options = array();
            }
            
            if (empty($options['connection_timeout']))
            {
                $options['connection_timeout'] = self::DEFAULT_CONNECTION_TIME_OUT;
            }
            
            try {
                self::$soapList[$key] = new SoapClient($wsdl, $options);
            } catch (SoapFault $fault) {
                $message = $fault->getMessage()."||".$fault->faultcode;
                return FALSE;
            }
        }
        return self::$soapList[$key];
    }
    /**
     * 发送soap请求 适合单参数的调用
     * 
     * @param string $apiName 方法名称
     * @param array $apiParam 参数
     * @param string $wsdl
     * @param array $options
     * @param string $message
     * 
     * @return 
     */
    protected static function soapRequest ($apiName, $apiParam, $wsdl, $options = array(), &$message)
    {
        if (! is_array($options)) {
            $options = array();
        }
        try {
            $soap = self::getSoapInc($wsdl, $options, $message);
            if ($soap === FALSE) {
                return FALSE;
            }
            $result = $soap->$apiName($apiParam);
        } catch (SoapFault $e) {
            $message = $e->getMessage();
            return FALSE;
        }
        return $result;
    }
    /**
     * Get Restful Interface Content
     *
     * @param string $url
     * @param string $message
     * @return boolean
     */
    protected function getRestfulContent ($url, &$message)
    {
        sdk_import('&.util.http.Request');
        $tmpUrl = explode("?", $url);
        if (!self::curlTest(array($tmpUrl[0]))) {
            $message = "不能连接到服务 ";
        	return false;
        }
        return Request::getContent($url, $message);
    }
    /**
     * write log
     *
     * @param string $s
     * @param string $server
     */
    protected function log ($s, $server, $success=false)
    {
        if (SDK_LOG_INFO) {
            if (! isset(self::$logger[$server]) || ! (self::$logger[$server] instanceof Logger)) {
                self::$logger[$server] = &SDK_Log::getLogInstance($server);
            }
            if(!$success || $success && LOG_SUCCESS) {
                self::$logger[$server]->info($s);
            }
        }
    }
    /**
     * 验证传递参数合法
     *
     * @param array $array
     * @param array $keys
     * @param string $message
     * @return Boolean
     */
    protected function _validateParam ($array, $keys, &$message)
    {
        if (is_array($keys)) {
            foreach ($keys as $item) {
                if (! isset($array[$item])) {
                    $message = "缺少 {$item} 参数";
                    return FALSE;
                }
            }
        }
        return TRUE;
    }
    protected static function curlTest($urlList, $rep=null)
    {
        foreach($urlList as $url)
        {
            if(!$url){
                break;
            }
            $userAgent = false;
            if(!strstr($url, "http://")) {
                $re = "/soap:address location=\"([^\"]+)\"/i";
                preg_match_all($re, file_get_contents($url), $arr);
                if($rep){
                    $url = strtr($arr[1][0], $rep);   
                }else{
                    $url = $arr[1][0]."?WSDL";    
                }
                $userAgent = true;
            }
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            if ($userAgent) {
            	curl_setopt($ch, CURLOPT_USERAGENT, 'chaoguoliuwei');
            } else {
                curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); 
            }           
            
            $content = curl_exec($ch);
            if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
                return FALSE;
            } 
            curl_close($ch);
        }
        
        return true;
    } 
}